package com.example.administrator.leargit.utils;

import com.example.administrator.leargit.extra.HttpCallbackLister;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2016/12/22 0022.
 */
public class HttpUtil {

    public static void   sendHttpRequest(final String address , final HttpCallbackLister listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    URL url = new URL(address);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setDoOutput(true);
                    connection.setDoInput(true);
                    connection.setReadTimeout(8000);
                    InputStream in = connection.getInputStream();
                    BufferedReader Reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = Reader.readLine()) != null) {
                        response.append(line);
                    }if (listener!=null){
                        listener.onFinish(response.toString());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    if (listener!=null){
                        listener.onError(e);
                    }
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }

                }
            }
        }).start();
    }
    public static void sendRequestWithHttpClient(){
        new Thread(new Runnable() {
            @Override
            public void run() {


            }
        }).start();

    }

}
